/**
 * Project: Daycare Web Application Package: com.made2ordersoftware.daycare.service File:
 * AbstractService_Client.java Created: Jun 29, 2010
 */

/*
 *  Copyright 2010 Made 2 Order Software, LLC
 *
 *  All rights reserved. No part of this software may be reproduced, stored in a retrieval system, or
 *  transmitted in any form or by any means, without the prior written permission of Made 2 Order Software.
 *
 *  You may not distribute the URL or redistribute the files to third parties. You may transfer the files
 *  onto a single storage device so long as you can prove ownership of the software. You may not reverse
 *  engineer, decompile, or disassemble the software or media files. You may not modify or translate the
 *  software or media, or distribute copies of the software or media without the written consent of Made
 *  2 Order Software
 */
package com.made2ordersoftware.service;

import com.sun.jersey.api.client.Client;
import com.sun.jersey.api.client.UniformInterfaceException;
import com.sun.jersey.api.client.WebResource;

/**
 * Jersey REST client generated for REST resource:AuthenticationSrvREST [authentication]<br>
 * USAGE:
 * <pre>
 *        AbstractService_Client client = new AbstractService_Client();
 *        Object response = client.XXX(...);
 *        // do whatever with response
 *        client.close();
 * </pre>
 *
 * @author David Canning
 */
public abstract class AbstractService_Client {

   private WebResource webResource;
   private Client client;

   /**
    * Creates a
    * <code>AbstractService_Client</code> entity.
    *
    * @param baseUri
    * @param subPath
    */
   public AbstractService_Client(String baseUri, String subPath) {
      com.sun.jersey.api.client.config.ClientConfig config =
            new com.sun.jersey.api.client.config.DefaultClientConfig();
      client = Client.create(config);
      webResource = client.resource(baseUri).path(subPath);

   }

   /**
    * Removes object with id from the database.
    *
    * @param id
    * @throws UniformInterfaceException
    */
   public void remove(String id) throws UniformInterfaceException {
      getWebResource().path(java.text.MessageFormat.format("{0}", new Object[]{id})).delete();
   }

   /**
    * Returns count of records in database.
    *
    * @return
    * @throws UniformInterfaceException
    */
   public String countREST() throws UniformInterfaceException {
      WebResource resource = getWebResource();
      resource = resource.path("count");
      return resource.accept(javax.ws.rs.core.MediaType.TEXT_PLAIN).get(String.class);
   }

   /**
    * Returns all object of type
    * <code>T</code> in XML format.
    *
    * @param <T>
    * @param responseType
    * @return
    * @throws UniformInterfaceException
    */
   public <T> T findAll_XML(Class<T> responseType) throws UniformInterfaceException {
      WebResource resource = getWebResource();
      return resource.accept(javax.ws.rs.core.MediaType.APPLICATION_XML).get(responseType);
   }

   /**
    * Returns all object of type
    * <code>T</code> in JSON format.
    *
    * @param <T>
    * @param responseType
    * @return
    * @throws UniformInterfaceException
    */
   public <T> T findAll_JSON(Class<T> responseType) throws UniformInterfaceException {
      WebResource resource = getWebResource();
      return resource.accept(javax.ws.rs.core.MediaType.APPLICATION_JSON).get(responseType);
   }

   /**
    * Persists the changes of object.
    *
    * @param requestEntity XML format of object.
    * @throws UniformInterfaceException
    */
   public void edit_XML(Object requestEntity) throws UniformInterfaceException {
      getWebResource().type(javax.ws.rs.core.MediaType.APPLICATION_XML).put(requestEntity);
   }

   /**
    * Persists the changes of object.
    *
    * @param requestEntity JSON format of object.
    * @throws UniformInterfaceException
    */
   public void edit_JSON(Object requestEntity) throws UniformInterfaceException {
      getWebResource().type(javax.ws.rs.core.MediaType.APPLICATION_JSON).put(requestEntity);
   }

   /**
    * Persists a newly created object.
    *
    * @param requestEntity XML format of object.
    * @throws UniformInterfaceException
    */
   public void create_XML(Object requestEntity) throws UniformInterfaceException {
      getWebResource().type(javax.ws.rs.core.MediaType.APPLICATION_XML).post(requestEntity);
   }

   /**
    * Persists a newly created object.
    *
    * @param requestEntity JSON format of object.
    * @throws UniformInterfaceException
    */
   public void create_JSON(Object requestEntity) throws UniformInterfaceException {
      getWebResource().type(javax.ws.rs.core.MediaType.APPLICATION_JSON).post(requestEntity);
   }

   /**
    * Returns object of type
    * <code>T</code> by id range in XML format.
    *
    * @param <T>
    * @param responseType
    * @param from
    * @param to
    * @return
    * @throws UniformInterfaceException
    */
   public <T> T findRange_XML(Class<T> responseType, String from, String to) throws
         UniformInterfaceException {
      WebResource resource = getWebResource();
      resource = resource.path(java.text.MessageFormat.format("{0}/{1}", new Object[]{from, to}));
      return resource.accept(javax.ws.rs.core.MediaType.APPLICATION_XML).get(responseType);
   }

   /**
    * Returns object of type
    * <code>T</code> by id range in JSON format.
    *
    * @param <T>
    * @param responseType
    * @param from
    * @param to
    * @return
    * @throws UniformInterfaceException
    */
   public <T> T findRange_JSON(Class<T> responseType, String from, String to) throws
         UniformInterfaceException {
      WebResource resource = getWebResource();
      resource = resource.path(java.text.MessageFormat.format("{0}/{1}", new Object[]{from, to}));
      return resource.accept(javax.ws.rs.core.MediaType.APPLICATION_JSON).get(responseType);
   }

   /**
    * Returns object of type
    * <code>T</code> by id in XML format.
    *
    * @param <T>
    * @param responseType
    * @param id
    * @return
    * @throws UniformInterfaceException
    */
   public <T> T find_XML(Class<T> responseType, String id) throws UniformInterfaceException {
      WebResource resource = getWebResource();
      resource = resource.path(java.text.MessageFormat.format("{0}", new Object[]{id}));
      return resource.accept(javax.ws.rs.core.MediaType.APPLICATION_XML).get(responseType);
   }

   /**
    * Returns object of type
    * <code>T</code> by id in JSON format.
    *
    * @param <T>
    * @param responseType
    * @param id
    * @return
    * @throws UniformInterfaceException
    */
   public <T> T find_JSON(Class<T> responseType, String id) throws UniformInterfaceException {
      WebResource resource = getWebResource();
      resource = resource.path(java.text.MessageFormat.format("{0}", new Object[]{id}));
      return resource.accept(javax.ws.rs.core.MediaType.APPLICATION_JSON).get(responseType);
   }

   /**
    *
    */
   public void close() {
      getClient().destroy();
   }

   /**
    * @return the webResource
    */
   protected WebResource getWebResource() {
      return webResource;
   }

   /**
    * @param webResource the webResource to set
    */
   protected void setWebResource(WebResource webResource) {
      this.webResource = webResource;
   }

   /**
    * @return the client
    */
   protected Client getClient() {
      return client;
   }

   /**
    * @param client the client to set
    */
   protected void setClient(Client client) {
      this.client = client;
   }
}
